package com.csu.csumall.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.csumall.entity.Order;
import com.csu.csumall.entity.User;
import com.csu.csumall.mapper.OrderMapper;
import com.csu.csumall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("orderService")
public class OrderServiceImp extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    private final OrderMapper orderMapper;

    @Autowired
    public OrderServiceImp(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Override
    public boolean isAuthenticated(User user, Integer orderId) {
        Order order =orderMapper.selectById(orderId);
        return (null != user.getId()) && (user.getId().equals(order.getUserId()));
    }

    @Override
    public List<Order> listOrder(User user, Integer status, int page, int size) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId())
                    .eq("status", status)
                    .orderByAsc("create_time");

        IPage<Order> orderIPage = page(new Page<>(page, size), queryWrapper);

        return orderIPage.getRecords();
    }

    @Override
    public List<Order> listOrder(User user, int page, int size) {

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id", user.getId())
                    .orderByAsc("create_time");

        IPage<Order> orderIPage = page(new Page<>(page, size), queryWrapper);
        return orderIPage.getRecords();
    }
}
